/**
 * 数据模型方法
 */
import LabBase from "./LabBase";

export default {
    mixins: [LabBase],
    data () {
        return {
            loading: true,
            modelInfo: {}
        }
    },
    methods: {
        loadModel() {
            const { callCmd, modelType } = this.element.props
            const { fields } = this.modelInfo
            if (!callCmd || !modelType) {
                this.loading = false
                return
            } else if (!this.$Util.isEmpty(fields)) {
                this.initElement(modelType)
                return;
            }
            this.loading = true
            this.$Util.cleanObj(this.modelInfo)
            this.$get('model/get/' + callCmd + '?appId=' + this.appInfo.appId)
                .then(res => {
                    this.$resultMsg(this, res, true)
                    if (res.code === 200) {
                        this.$Util.copyAttr(res.data, this.modelInfo,null, this)
                        const { tables, fields, abilitiyList, joins } = res.data
                        this.$set(this.modelInfo, 'tables', tables)
                        this.$set(this.modelInfo, 'fields', fields)
                        this.$set(this.modelInfo, 'joins', joins)
                        this.$set(this.modelInfo, 'abilitiyList', abilitiyList)
                        this.initElement(this.element.props.modelType)
                    }
                })
        },
        initElement(modelType) {
            const { callCmd, fields, modelPrimary, tables } = this.modelInfo
            const sourceRequest =  {trigger: 'source', requestType: 'POST', requestUrl: '/model/queryData', resultProp: 'data', totalProp: 'total', dataProp: 'records', bodyFilter: 'filters',
                params: [
                    { label: '调用指令', location: 'body', prop: 'callCmd', value: callCmd, type: 'String' },
                    { label: '页码',location: 'body', prop: 'page', value: 1, type: 'Number' },
                    { label: '每页数量',location: 'body', prop: 'pageSize', value: 10, type: 'Number' }
                ],
                filters: []
            }
            const sortTables =  tables.sort((a, b) => a.jdbcOrder - b.jdbcOrder)
            const buttonGroup = this.$LabRender.create('buttonGroup','','form','btnGroup');
            buttonGroup.props.showCount = 5
            this.element.elements.splice(0, this.element.elements.length)
            this.element.requests.splice(0, this.element.requests.length)
            this.element.render = modelType
            const jdbcKeys = []
            if (modelType === 'form') {
                this.element.mapper = modelType
                sourceRequest.resultProp = 'data.records'
                this.$set(this.element.props, 'rowKey',modelPrimary)
                this.element.requests.push(sourceRequest)
                sortTables.forEach(table => {
                    let tableFields = fields.filter(item => item.jdbcTable === table.jdbcName)
                    if (tableFields.length > 0) {
                        if (table.jdbcComment) {
                            this.element.elements.push(this.$LabRender.create('divider', table.jdbcComment))
                        }
                        tableFields = tableFields.sort((a,b) => a.jdbcOrder - b.jdbcOrder )
                        tableFields.forEach(item => {
                            this.transfer(item, this.element,jdbcKeys)
                        })
                    }
                })
                buttonGroup.elements.push(this.$LabRender.createBtn('保存','form','btnSave',[{ trigger: 'click', callMethod: 'lab.from.save' }], { type: 'success', icon: 'el-icon-check'}))
                this.element.elements.push(buttonGroup);
            } else {
                if (modelType === 'manager') {
                    buttonGroup.elements.push(this.$LabRender.createBtn('添加','form','btnAdd',[{ trigger: 'click', callMethod: 'lab.table.add' }], { type: 'primary', icon: 'el-icon-circle-plus-outline'}))
                    buttonGroup.elements.push(this.$LabRender.createBtn('保存','form','btnSave',[{ trigger: 'click', callMethod: 'lab.table.save' }], { type: 'success', icon: 'el-icon-check'}))
                    buttonGroup.elements.push(this.$LabRender.createBtn('删除','form','btnRemove',[{ trigger: 'click', callMethod: 'lab.table.remove' }], { type: 'danger', icon: 'el-icon-delete'}))
                    buttonGroup.elements.push(this.$LabRender.createBtn('导出','form','btnExport',[{ trigger: 'click', callMethod: 'lab.table.export' }], { type: 'warning', icon: 'el-icon-download'}))
                    buttonGroup.elements.push(this.$LabRender.createBtn('导入','form','btnImport',[{ trigger: 'click', callMethod: 'lab.table.import' }], { type: 'info', icon: 'el-icon-upload2'}))
                    this.element.elements.push(buttonGroup);
                }
                const renderElement = { render: 'table',  mapper: 'table',elements: [], props: { rowKey: modelPrimary }, requests: [sourceRequest] }
                fields.forEach(item => {
                    this.transfer(item, renderElement,jdbcKeys)
                })
                this.$LabRender.autoFill(renderElement, this)
                this.element.elements.push(renderElement)
            }
            this.modelCallBack(this.element)
        },
        modelCallBack(element) {
            console.info('糊掉')
        },
        transfer(field, renderElement,jdbcKeys) {
            const { jdbcName, jdbcAlias, jdbcComment, jdbcType, jdbcKey, jdbcDigit,callDict } = field
            if (jdbcKey && !jdbcKeys.includes(jdbcName)) {
                jdbcKeys.push(jdbcName)
            }
            if (jdbcKeys.includes(jdbcName) || ['createAt','createById','createByName','updateAt','updateById','updateByName','branchId','groupId'].includes(jdbcName)) {
                return
            }
            const ele = { label: jdbcComment, mapper: jdbcName || jdbcAlias, props: { tableFilter: 'like' }, requests: [] }
            const index = renderElement.elements.findIndex(item => item.mapper === ele.mapper)
            if (index > -1) {
                return;
            }
            if (['bigint','int'].includes(jdbcType)) {
                ele.render = 'number'
                ele.props.tableFilter = '='
            } else if (['decimal'].includes(jdbcType)) {
                ele.render = 'number'
                ele.props.tableFilter = '='
                ele.props.precision = jdbcDigit
            } else if (['date'].includes(jdbcType)) {
                ele.render = 'date'
                ele.props.tableFilter = 'between'
            } else if (['datetime'].includes(jdbcType)) {
                ele.render = 'datetime'
                ele.props.tableFilter = 'between'
            } else if (['bit'].includes(jdbcType)) {
                ele.render = 'switch'
                ele.props.tableFilter = '='
            } else if (['text'].includes(jdbcType)) {
                ele.render = 'textarea'
            } else {
                ele.render = 'text'
            }
            if (callDict) {
                ele.render = 'select'
                ele.requests.push({trigger: 'source', requestType: 'GET', requestUrl: '/labDict/load/' + callDict, cacheKey: callDict,
                    resultProp: 'data', labelProp: 'dataLabel', valueProp: 'dataValue', codeProp: 'dataCode', searchProp: 'dataLabel'
                })
            }
            this.$LabRender.autoFill(ele, this)
            renderElement.elements.push(ele)
        }
    }
}
